/*
 * mox_compat.h
 *
 *  Created on: Jun 21, 2010
 *      Author: Jason Monk
 */
#ifndef MOX_COMPAT_H_
#define MOX_COMPAT_H_

#ifndef __XC__
#include <xccompat.h>
#endif

#ifdef __XC__
#define SCHEND streaming chanend
#else
#define SCHEND chanend
#endif

//Generic Resource Tools
#define RES_IN_USE(x) asm("setc res[%0],0x08"::"r"(x))
#define OPNOPNOP(x) do {\
						asm("nop");\
						asm("nop");\
					} while (0)

//Port Specific Tools
#define PORT_SET_BUFF(x) asm("setc res[%0], 0x200f"::"r"(x))
#define PORT_SIZE_BUFF(x,y) asm("settw res[%0], %1"::"r"(x),"r"(y))
#define PORT_USE_BUFFERED(x,y) do {\
					register int tmp = y;\
					RES_IN_USE(x);\
					PORT_SET_BUFF(x);\
					PORT_SIZE_BUFF(x,tmp);\
					} while (0)
void port_out(port p, unsigned val);
unsigned port_in(port p);

//Lock Specific Tools
typedef unsigned lock;
#define GET_LOCK(x) 	asm("getr %0,0x05":"=r"(x):)
#define LOCK_GRAB(x) 	asm("in %0,res[%0]"::"r"(x))
#define LOCK_RLEASE(x) 	asm("out res[%0],%0"::"r"(x))
#define FREE_LOCK(x)	asm("freer res[%0]"::"r"(x))

//Generic Chanend Specific Tools
void ochan(SCHEND c, unsigned val);
unsigned ichan(SCHEND c);

void nchan_out(chanend c, unsigned val);
unsigned nchan_in(chanend c);

#endif //MOX_COMPAT_H_
